﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web.Mvc;

namespace NotWindowsStore.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/

        public ActionResult Index()
        {
            using (var ctx = new Models.notwindowsentities())
            {
                return Json(ctx.NwsCategories.Select(m => new { m.CategoryId, m.Title }).ToList(), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Image(int id)
        {
            using (var ctx = new Models.notwindowsentities())
            {
                var cat = ctx.NwsCategories.First(i => i.CategoryId == id);

                using (var bm = new Bitmap(150, 150))
                using (var g = Graphics.FromImage(bm))
                using (var brush = new SolidBrush(Color.FromArgb(255, 0, 179, 212)))
                using (var textBrush = new SolidBrush(Color.White))
                using (var font = new Font(FontFamily.GenericSansSerif, 14))
                {
                    g.FillRectangle(brush, 0, 0, 150, 150);
                    g.DrawString(cat.Title, font, textBrush, 5, 120);
                    Response.ContentType = "image/png";

                    using (var memStream = new MemoryStream())
                    {
                        bm.Save(memStream, ImageFormat.Png);
                        Response.BinaryWrite(memStream.ToArray());
                    }
                    return null;
                }
            }
        }

    }
}
